package tsi.trabalho.truco;

import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Jogo extends Activity {
  
/** Called when the activity is first created. */
	

	private static BluetoothSocket socket;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jogo);
    findViewById(R.id.imageView1).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.mesa).setOnDragListener(new MyDragListener());
    findViewById(R.id.mao).setOnDragListener(new MyDragListener());
    findViewById(R.id.monte).setOnDragListener(new MyDragListener());

  }

  private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.VISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

  class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.card_1);
    Drawable normalShape = getResources().getDrawable(R.drawable.card_1);

    @Override
    public boolean onDrag(View v, DragEvent event) {
      int action = event.getAction();
      switch (event.getAction()) {
      case DragEvent.ACTION_DRAG_STARTED:
//    	  Log.i("JOGO", "ACTION_DRAG_STARTED");
        // do nothing
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
    	  Log.i("JOGO", "ACTION_DRAG_ENTERED");
          ImageView image = (ImageView)v;
          Log.i("CART", image.getId()+"");
          
        break;
      case DragEvent.ACTION_DRAG_EXITED:
//    	  Log.i("JOGO", "ACTION_DRAG_EXITED");
        break;
      case DragEvent.ACTION_DROP:
//    	  Log.i("JOGO", "ACTION_DROP");
        // Dropped, reassign View to ViewGroup
        View view = (View) event.getLocalState();
        ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        LinearLayout container = (LinearLayout) v;
        container.addView(view);
        view.setVisibility(View.VISIBLE);
        break;
      case DragEvent.ACTION_DRAG_ENDED:
//    	  Log.i("JOGO", "ACTION_DRAG_ENDED");
//        v.setBackgroundDrawable(normalShape);
      default:
        break;
      }
      return true;
    }
  }
  
//  --------------------------------------------------------------------
  
  public static void setBluetoothSocket(BluetoothSocket _BTSocket)
	{
		socket=_BTSocket;
	}
  
} 